// pages/phoneNum/phoneNum.js
import {
  request
} from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: '',
    num: '',
    numback: '',
    user: {},
    pass: '',
    repass: '',
    reg1: false,
    reg2: false,
    //设置初始的状态、包含字体、颜色、还有等待事件 > <
    sendTime: '获取验证码',
    sendColor: '#363636',
    snsMsgWait: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = typeof wx.getStorageSync('user') === 'string' ? JSON.parse(wx.getStorageSync('user')) : wx.getStorageSync('user');
    console.log(user);
    this.setData({
      user,
    });
  },
  Num(e) {
    this.setData({
      num: e.detail.value
    })
  },
  pass(e) {
    this.setData({
      pass: e.detail.value
    })
  },
  repass(e) {
    this.setData({
      repass: e.detail.value
    })
  },

  //点击获取验证码触发的函数
  getPhone(e) {
    var inter = setInterval(function () {
      this.setData({
        smsFlag: true,
        sendColor: '#cccccc',
        sendTime: this.data.snsMsgWait + 's后重发',
        snsMsgWait: this.data.snsMsgWait - 1
      });
      if (this.data.snsMsgWait < 0) {
        clearInterval(inter)
        this.setData({
          sendColor: '#363636',
          sendTime: '获取验证码',
          snsMsgWait: 60,
          smsFlag: false
        });
      }
    }.bind(this), 1000);
    request({
      url:'user/random',
      data: {
        "user_phone": this.data.user.safetyInfo.alipay_account
      },
    }).then((result) => {
      console.log(result);
      this.setData({
        numback: result.data
      })
    });
  },
  // 输入密码框失去焦点时验证
  passblur() {
    let reg = /^\d{1,6}$/;
    if (!reg.test(this.data.pass)) {
      this.getShowToast('请设置密码格式为六位数字');
    } else {
      this.setData({
        reg1: true
      })
    }
  },

  // 重新输入密码验证
  repassblur() {
    if (this.data.pass !== this.data.repass) {
      this.getShowToast('两次输入的密码应该一致');
    } else {
      this.setData({
        reg2: true
      })
    }
  },

  getShowToast(text) {
    wx.showToast({
      title: text,
      duration: 2000,
      icon: 'none'
    })
  },

  //输入验证码之后点击确定按钮触发的函数
  goClick(e) {

    if (this.data.num != this.data.numback) {
      this.getShowToast('验证码输入不正确');
    }else if (this.data.num === '' || this.data.pass === '' || this.data.repass === '') {
      this.getShowToast('不能为空');
    } else if(!this.data.reg2){
      this.getShowToast('两次输入的密码应该一致');
    } 
    if (this.data.reg1 === true && this.data.reg2 === true) {
      console.log(111);
      request({
        url:'safetyInfo/updPayPassword',
        data: {
          open_id: this.data.user.safetyInfo.open_id,
          pay_password: this.data.pass
        },
      }).then((result) => {
        console.log(result);
        if (result.data.code == '0') {
          wx.setStorageSync('user', result.data.data);
          this.setData({
            user:result.data.data
          })
          wx.navigateBack({
            delta: 1
          })
        }
      });
    }
  },
})